﻿using GameEntitySystem;
using Game;
using Engine;

namespace SCIENEW
{
	public class SorterDeviceBehavior : EntityDeviceBehavior<ComponentChest>
	{
		public SorterDeviceBehavior(Project project) : base(project)
		{
			m_subsystemPickables = project.FindSubsystem<SubsystemPickables>(true);
			m_subsystemProjectiles = project.FindSubsystem<SubsystemProjectiles>(true);
			//m_subsystemEnginePower = project.FindSubsystem<SubsystemEnginePower>(true);
			m_subsystemTerrain = project.FindSubsystem<SubsystemTerrain>(true);
		}
		public SubsystemPickables m_subsystemPickables;
		public SubsystemProjectiles m_subsystemProjectiles;
		public SubsystemTerrain m_subsystemTerrain;

		public override void OnHitByProjectile(CellFace cellFace, WorldItem worldItem)
		{
			if (worldItem.ToRemove)
			{
				return;
			}
			ComponentBlockEntity blockEntity = m_subsystemBlockEntities.GetBlockEntity(cellFace.X, cellFace.Y, cellFace.Z);
			if (blockEntity != null)
			{
				var pickable = worldItem as Pickable;
				int num = pickable?.Count ?? 1;
				if (num > 1)
				{
					return;
				}
				var position = new Vector3(cellFace.Point) + new Vector3(0.5f);
				ComponentChest inventory = blockEntity.Entity.FindComponent<ComponentChest>(true);
				Vector3 v;
				if (inventory.GetSlotValue(0) == worldItem.Value)
				{
					worldItem.ToRemove = true;
					v = new Vector3(-1f, 0f, 0f);
					if (m_subsystemProjectiles.FireProjectile(worldItem.Value, position + 0.75f * v, 10f * v, Vector3.Zero, null) == null)
					{
						m_subsystemPickables.AddPickable(worldItem.Value, 1, position + 0.75f * v, 1f * v, null);
					}
					return;
				}
				if (inventory.GetSlotValue(1) == worldItem.Value)
				{
					worldItem.ToRemove = true;
					v = new Vector3(0f, 0f, 1f);
					if (m_subsystemProjectiles.FireProjectile(worldItem.Value, position + 0.75f * v, 10f * v, Vector3.Zero, null) == null)
					{
						m_subsystemPickables.AddPickable(worldItem.Value, 1, position + 0.75f * v, 1f * v, null);
					}
					return;
				}
				if (inventory.GetSlotValue(2) == worldItem.Value)
				{
					worldItem.ToRemove = true;
					v = new Vector3(1f, 0f, 0f);
					if (m_subsystemProjectiles.FireProjectile(worldItem.Value, position + 0.75f * v, 10f * v, Vector3.Zero, null) == null)
					{
						m_subsystemPickables.AddPickable(worldItem.Value, 1, position + 0.75f * v, 1f * v, null);
					}
					return;
				}
				if (inventory.GetSlotValue(3) == worldItem.Value)
				{
					worldItem.ToRemove = true;
					v = new Vector3(0f, 0f, -1f);
					if (m_subsystemProjectiles.FireProjectile(worldItem.Value, position + 0.75f * v, 10f * v, Vector3.Zero, null) == null)
					{
						m_subsystemPickables.AddPickable(worldItem.Value, 1, position + 0.75f * v, 1f * v, null);
					}
					return;
				}
				v = -CellFace.FaceToVector3(cellFace.Face);
				worldItem.ToRemove = true;
				if (m_subsystemProjectiles.FireProjectile(worldItem.Value, position + 0.75f * v, 10f * v, Vector3.Zero, null) == null)
				{
					m_subsystemPickables.AddPickable(worldItem.Value, 1, position + 0.75f * v, 1f * v, null);
				}
				return;
			}
		}

		public override void OnCollide(CellFace cellFace, float velocity, ComponentBody componentBody)
		{
			int x = cellFace.X,
				y = cellFace.Y,
				z = cellFace.Z;
			if (m_subsystemTerrain.Terrain.GetCellContentsFast(x, y, z) != IceBlock.Index || componentBody.Mass < 999f || componentBody.Density > 1f)
				return;
			m_subsystemTerrain.DestroyCell(0, x, y, z, WaterBlock.Index, false, false);
		}
	}
}